package cn.imjzm.runcarserve.controller;

import cn.imjzm.runcarserve.pojo.Result;
import cn.imjzm.runcarserve.pojo.User;
import cn.imjzm.runcarserve.pojo.carinformation;
import cn.imjzm.runcarserve.pojo.news;
import cn.imjzm.runcarserve.service.HomeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@CrossOrigin(origins = "*")
@RestController
@Slf4j
@Tag(name = "首页接口", description = "获取小车状态信息和消息通知相关接口")
public class HomeController {

    @Autowired
    public HomeService homeService;

    /**
     * 返回电量、信号、温度信息
     * @param uId 用户ID
     * @return 小车状态信息
     */
    @Operation(summary = "查询小车状态信息", description = "根据用户ID查询电池电量、信号强度、温度等信息")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功，返回小车状态信息"),
            @ApiResponse(responseCode = "400", description = "请求参数错误"),
            @ApiResponse(responseCode = "404", description = "未找到对应用户信息"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @GetMapping("/carInfo/{uId}")
    public Result getCarInfo(
            @Parameter(description = "用户ID", example = "123", required = true)
            @PathVariable Integer uId) {

        log.info("查询电量信息：{}", uId);
        carinformation CarInfo = homeService.selectCarInfo(uId);
        return Result.success(CarInfo);
    }

    /**
     * 获取消息通知列表
     * @param email 用户邮箱
     * @return 消息列表
     */
    @Operation(summary = "查询消息通知", description = "根据用户手机号查询消息通知列表")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功，返回消息列表"),
            @ApiResponse(responseCode = "400", description = "请求参数错误"),
            @ApiResponse(responseCode = "404", description = "未找到对应手机号消息"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @GetMapping("/newList/{email}")
    public Result getNewList(
            @Parameter(description = "用户邮箱", example = "130213@qq.com", required = true)
            @PathVariable String email) {

        log.info("查询消息通知，用户Id：{}", email);
        List<news> ListNews = homeService.getNewList(email);
        return Result.success(ListNews);
    }

    /**
     * 修改用户信息（参数回显）
     * @param id
     * @return
     */
    @GetMapping("/update/{id}")
    public Result getHome(@PathVariable int id){
        log.info("用户ID：{}",id);
        User user = homeService.getHome(id);
        return Result.success(user);
    }

    /**
     * 修改用户信息(用户名)
     * @param user
     * @return
     */
    @Operation(summary = "修改用户信息", description = "根据用户ID修改用户信息")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "修改成功"),
            @ApiResponse(responseCode = "400", description = "请求参数错误"),
            @ApiResponse(responseCode = "404", description = "未找到对应用户"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @PutMapping("/update")
    //解决跨域问题
    @CrossOrigin
    public Result update(@RequestBody User user){
        log.info("修改用户信息：{}",user);
        homeService.update(user);
        return Result.success();
    }

    /**
     * 修改用户信息(用户名)
     * @param carName
     * @return
     */
    @Operation(summary = "修改用户信息", description = "根据用户ID修改用户信息")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "修改成功"),
            @ApiResponse(responseCode = "400", description = "请求参数错误"),
            @ApiResponse(responseCode = "404", description = "未找到对应用户"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @PutMapping("/updateCarName")
    //解决跨域问题
    @CrossOrigin
    public Result updateCarName(@RequestBody User carName){
        log.info("修改小车名称：{}",carName);
        homeService.updateCarName(carName);
        return Result.success();
    }

}
